package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration {

    private static final Properties P = new Properties();

    static {
        loadProperties(Constants.PROPERTIES_FILE);
    }

    public static boolean loadProperties(String path) {
        boolean flag = false;
        Properties properties = P;
        InputStream is = null;
        try {
            is = Configuration.class.getResourceAsStream("/" + path);
            if (null == is) {
                throw new RuntimeException(String.format("找不到配置文件：[%1$s]", path));
            }
            properties.load(is);
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(is);
        }
        return flag;
    }

    public static String getProperty(String name) {
        String s = P.getProperty(name);
        if (null == s) {
            throw new RuntimeException(String.format("配置不完整：[%1$s]", name));
        }
        return s;
    }
    
    public static String getProperty(String name, String defaultValue) {
        String s = P.getProperty(name);
        if (null == s) {
            s = defaultValue;
        }
        return s;
    }
    
    public static void main(String[] args) {
        Configuration.getProperty("sdfaf");
    }
}
